Completed
Push — master ( c8749b...21bf41 )
by Dongxin
32s
created

render.js ➔ onParse   C

Complexity

Conditions 14
Paths 8

Size

Total Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 14
c 2
b 0
f 1
nc 8
nop 1
dl 0
loc 71
rs 5.5568

1 Function

Rating   Name   Duplication   Size   Complexity  
B render.js ➔ ... ➔ init 0 34 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like render.js ➔ onParse often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
// Copyright © 2017 TangDongxin
2
3
// Permission is hereby granted, free of charge, to any person obtaining
4
// a copy of this software and associated documentation files (the "Software"),
5
// to deal in the Software without restriction, including without limitation
6
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7
// and/or sell copies of the Software, and to permit persons to whom the
8
// Software is furnished to do so, subject to the following conditions:
9
10
// The above copyright notice and this permission notice shall be included
11
// in all copies or substantial portions of the Software.
12
13
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
18
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
19
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21
// ===========================================
22
// JSON PARSER
23
// ===========================================
24
function onParse(result) {
0 ignored issues
show
Unused Code introduced by
The parameter result is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
25
    var chrome = this.chrome || this.browser;
0 ignored issues
show
Unused Code introduced by
The assignment to variable chrome seems to be never used. Consider removing it.
Loading history...
26
    var jsonRe = /^\s*(?:\[\s*(?=-?\d|true|false|null|["[{])[^]*\]|\{\s*"[^]+\})\s*$/;
27
    var body = document.body;
28
    var str, jsonpMatch, tag;
29
30
    if (strictOnly) {
0 ignored issues
show
Best Practice introduced by
If you intend to check if the variable strictOnly is declared in the current environment, consider using typeof strictOnly === "undefined" instead. This is safe if the variable is not actually declared.
Loading history...
31
        // only render when the contentType is json
32
        if (/[+\/]json$/i.test(document.contentType)) {
33
            init();
34
            str = body.textContent;
35
            draw(str, body);
36
        }
37
    } else {
38
        // check whether the content is json or like json
39
        first = body && body.firstChild;
0 ignored issues
show
Bug introduced by
The variable first seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.first.
Loading history...
40
        if (first &&
41
            (first.tagName == "PRE" &&
42
                first == body.lastElementChild ||
43
                first == body.lastChild &&
44
                first.nodeType == 3) &&
45
            (str = first.textContent) &&
46
            (/[+\/]json$/i.test(document.contentType) ||
47
                (jsonpMatch = /^\s*((?:\/\*\*\/\s*)?([$a-z_][$\w]*)\s*(?:&&\s*\2\s*)?\()([^]+)(\)[\s;]*)$/i.exec(str)) &&
48
                jsonRe.test(jsonpMatch[3]) || jsonRe.test(str))) {
49
            if (jsonpMatch) {
50
                str = jsonpMatch[3];
51
                body.replaceChild(fragment(jsonpMatch[1], jsonpMatch[4]), first);
52
                first = body.lastChild.previousSibling;
53
            }
54
            init();
55
            draw(str, body);
56
        }
57
    }
58
59
60
    function init() {
61
        if (tag) {
62
            return;
63
        }
64
65
        tag = document.createElement("style");
66
        tag.textContent = [
67
            '.R', ',.D', '{font:' + fontSize + ' ' + fontStyle + '}' +
0 ignored issues
show
Bug introduced by
The variable fontSize seems to be never declared. If this is a global, consider adding a /** global: fontSize */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
The variable fontStyle seems to be never declared. If this is a global, consider adding a /** global: fontStyle */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
68
            '.D', '{margin-left:6px; padding-left:1em; margin-top: 1px; border-left:1px dashed; border-color: #93A1A1;}' +
69
            '.X', '{border:1px solid #ccc; padding:1em}' +
70
            'a.L', '{text-decoration:none}' +
71
            'a.L', ':hover,a.L', ':focus{text-decoration:underline}' +
72
            'i.I', '{cursor:pointer;color:#ccc}' +
73
            'i.H', ',i.I', ':hover{text-shadow: 1px 1px 3px #999; color:#333}' +
74
            'i.I', ':before{content:" ▼ "}' +
75
            'i.C', ':before{content:" ▶ "}' +
76
            'i.I', ':after{content:attr(data-content)}' +
77
            'i.C', '+.D', '{width:1px; height:1px; margin:0; padding:0; border:0; display:inline-block; overflow:hidden}' +
78
            '.S', '{color:' + strColor + '}' + // string
0 ignored issues
show
Bug introduced by
The variable strColor seems to be never declared. If this is a global, consider adding a /** global: strColor */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
79
            '.K', '{color:' + keyColor + '}' + // key
0 ignored issues
show
Bug introduced by
The variable keyColor seems to be never declared. If this is a global, consider adding a /** global: keyColor */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
80
            '.E', '{color:#BCADAD}' + // error
81
            '.B', '{color:' + intColor + '}' + // number and bool
0 ignored issues
show
Bug introduced by
The variable intColor seems to be never declared. If this is a global, consider adding a /** global: intColor */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
82
            '.E', ',.B', '{font-style: italic}' + // number bold
83
            'h3.E', '{margin:0 0 1em}'
84
        ].join(RAND);
0 ignored issues
show
Bug introduced by
The variable RAND seems to be never declared. If this is a global, consider adding a /** global: RAND */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
85
86
        tag.textContent = tag.textContent + 'body {background: ' + bgColor + '; color:' + defaultColor + ';}';
0 ignored issues
show
Bug introduced by
The variable bgColor seems to be never declared. If this is a global, consider adding a /** global: bgColor */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
The variable defaultColor seems to be never declared. If this is a global, consider adding a /** global: defaultColor */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
87
        tag.textContent = tag.textContent + '.HIDE {width:200px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }';
88
        tag.textContent = tag.textContent + '* {font:' + fontSize + ' ' + fontStyle + ' !important;}';
89
        tag.textContent = tag.textContent + '.black_overlay{ display: none; position: absolute; top: 0%; left: 0%; width: 100%; height: 100%; background-color: black; z-index:1001; -moz-opacity: 0.6; opacity:.60; filter: alpha(opacity=60); }';
90
        tag.textContent = tag.textContent + '.white_content { display: none; position: absolute; top: 25%; left: 25%; max-width: 60%; max-height: 80%; padding: 16px; border: 16px solid orange; background-color: white; z-index:1002; overflow: auto; }';
91
92
        document.head.appendChild(tag);
93
    }
94
}
95
96